<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="车辆编号" prop="carNumber">
        <el-input
          v-model.trim="queryParams.carNumber"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌照" prop="carLicensePlate" label-width="56px">
        <el-input
          v-model.trim="queryParams.carLicensePlate"
          placeholder="请输入车牌照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加油日期" prop="refuelingTime">
        <el-date-picker
          type="date"
          placeholder="请选择"
          v-model="queryParams.refuelingTime"
          value-format="timestamp"
          @change="handleQuery"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:refueling:add']"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="refuelingList"
      max-height="430"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="ID" align="center" prop="id" fixed="left" />
      <el-table-column
        label="车辆编号"
        align="center"
        prop="carNumber"
        min-width="140"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="车牌照"
        align="center"
        min-width="140"
        :show-overflow-tooltip="true"
        prop="carLicensePlate"
      />
      <el-table-column
        label="加油日期"
        align="center"
        prop="refuelingTime"
        min-width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.refuelingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="加油升数"
        align="center"
        prop="refuelingVolume"
        min-width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="加油单价"
        align="center"
        prop="unitPrice"
        min-width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="加油总价"
        align="center"
        prop="totalCost"
        min-width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="供应商"
        align="center"
        prop="provider"
        min-width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        min-width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="最后编辑时间"
        align="center"
        prop="updateTime"
        min-width="160"
      >
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="120"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆加油记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="78px">
        <el-form-item label="车牌照" prop="carLicensePlate">
          <el-select
            v-model="form.carLicensePlate"
            filterable
            style="width: 100%"
            @change="handleCarChange"
          >
            <el-option
              v-for="item in carList"
              :key="item.id"
              :label="item.carLicensePlate"
              :value="item.carLicensePlate"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="车辆编号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车辆编号" />
        </el-form-item> -->

        <el-form-item label="加油时间" prop="refuelingTime">
          <el-date-picker
            type="date"
            style="width: 100%"
            v-model="form.refuelingTime"
            value-format="timestamp"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="加油升数" prop="refuelingVolume">
          <el-input
            v-model="form.refuelingVolume"
            placeholder="请输入加油升数"
          />
        </el-form-item>
        <el-form-item label="加油单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入加油单价" />
        </el-form-item>
        <el-form-item label="加油总价" prop="totalCost">
          <el-input v-model="form.totalCost" placeholder="请输入加油总价" />
        </el-form-item>
        <el-form-item label="供应商" prop="provider">
          <el-input v-model="form.provider" placeholder="请输入供应商" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRefueling,
  getRefueling,
  delRefueling,
  addRefueling,
  updateRefueling,
} from "@/api/service/refueling";
import { listCars } from "@/api/service/cars";
import { formatDate } from "@/utils";
export default {
  name: "Refueling",
  data() {
    return {
      carList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆加油记录表格数据
      refuelingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        carNumber: null,
        carLicensePlate: null,
        refuelingTime: null,
        refuelingVolume: null,
        unitPrice: null,
        totalCost: null,
        provider: null,
        isDeleted: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carId: [{ required: true, message: "车辆id不能为空", trigger: "blur" }],
        // carNumber: [
        //   { required: true, message: "车辆编号不能为空", trigger: "blur" },
        // ],
        carLicensePlate: [
          { required: true, message: "车牌照不能为空", trigger: "blur" },
        ],
        refuelingTime: [
          { required: true, message: "加油时间不能为空", trigger: "blur" },
        ],
        refuelingVolume: [
          { required: true, message: "加油升数不能为空", trigger: "blur" },
        ],
        unitPrice: [
          { required: true, message: "加油单价不能为空", trigger: "blur" },
        ],
        totalCost: [
          { required: true, message: "加油总价不能为空", trigger: "blur" },
        ],
        provider: [
          { required: true, message: "供应商不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        isDeleted: [
          {
            required: true,
            message: "0:未删除/1:已删除不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    open(n, o) {
      if (n) {
        this.getCarList();
      }
    },
  },
  methods: {
    getCarList() {
      listCars({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.carList = res.rows;
      });
    },
    handleCarChange(val) {
      this.form.carNumber = this.carList.find(
        (s) => s.carLicensePlate === val
      )?.carNumber;
    },
    formatDate,
    /** 查询车辆加油记录列表 */
    getList() {
      this.loading = true;
      listRefueling(this.queryParams).then((response) => {
        this.refuelingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        carNumber: null,
        carLicensePlate: null,
        refuelingTime: null,
        refuelingVolume: null,
        unitPrice: null,
        totalCost: null,
        provider: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDeleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增车辆加油记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRefueling(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆加油记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRefueling(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefueling(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('确认要删除吗？')
        .then(function () {
          return delRefueling(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/refueling/export",
        {
          ...this.queryParams,
        },
        `refueling_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
